package com.zh.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.Result;
import com.zh.dto.DishDto;

import com.zh.entity.Category;
import com.zh.entity.Dish;
import com.zh.entity.DishFlavor;
import com.zh.service.CategoryService;
import com.zh.service.DishFlavorsService;
import com.zh.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 菜品管理 前端控制器
 * </p>
 *
 * @author 你爹
 * @since 2023-09-21
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishFlavorsService dishFlavorsService;
    @GetMapping("/page")
    public Result<Page> page(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize, String name) {
        log.info("分页查询: page={},pageSize={},name={}", page, pageSize, name);
        Page<Dish> p = new Page<>(page, pageSize);
        Page<DishDto> pageInfo = new Page<>(page, pageSize);
        LambdaUpdateWrapper<Dish> wrapper = new LambdaUpdateWrapper<>();
        wrapper.like(name != null, Dish::getName, name);
        wrapper.orderByDesc(Dish::getUpdateTime);
        dishService.page(p, wrapper);
        //拷贝到 dto
        BeanUtils.copyProperties(p, pageInfo, "records");
        List<Dish> dishes = p.getRecords();
        List<DishDto> dishDtoList = dishes.stream().map(dish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            dishDto.setCategoryName(categoryService.getById(dish.getCategoryId()).getName());
            return dishDto;
        }).collect(Collectors.toList());
        pageInfo.setRecords(dishDtoList);
        log.info("最终结果:{}",pageInfo.getRecords());
        return Result.success(pageInfo);
    }

    @PostMapping
    public Result save(@RequestBody DishDto dishDto) {
        log.info("dishDto={}", dishDto);
        dishService.saveWithFlavors(dishDto);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result<DishDto> getById(@PathVariable Long id){
        log.info("查询id为{}的dish",id);
        DishDto dishDto = dishService.getDishById(id);
        log.info("查询到的dishDto:{}",dishDto);
        return Result.success(dishDto);
    }

//    /**
//     * 根据分类id/ 菜品名称查dishs
//     * @param
//     * @return
//     */
//    @GetMapping("/list")
//    public Result<List<Dish>> list(Dish dish){
//        log.info("查询分类id为{}/name={}的dish",dish.getCategoryId(),dish.getName());
//        LambdaUpdateWrapper<Dish> wrapper= new LambdaUpdateWrapper<>();
//        wrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
//        wrapper.like(dish.getName()!=null,Dish::getName,dish.getName());
//        //移动端需要只显示status==1
//        wrapper.eq(dish.getStatus()!=null,Dish::getStatus,dish.getStatus());
//        wrapper.orderByDesc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
//        List<Dish> dishList = dishService.list(wrapper);
//        return Result.success(dishList);
//    }

    /**
     * 更新dish list方法
     * 根据分类id/ 菜品名称查dishs
     * @param
     * @return
     */
    @GetMapping("/list")
    public Result<List<DishDto>> list(Dish dish){
        log.info("查询分类id为{}/name={}的dish",dish.getCategoryId(),dish.getName());
        List<DishDto> dishList = dishService.listWithFlavors(dish);
        return Result.success(dishList);
    }

    @PutMapping
    public Result update(@RequestBody DishDto dishDto){
        log.info("更新dish dishDto={}",dishDto);
        dishService.updateWithFlavors(dishDto);
        return Result.success();
    }

    /**
     * （批量）起售/停售
     */
    @PostMapping("/status/{status}")
    public Result status(@PathVariable Integer status, @RequestParam("ids") List<Long> ids){
        log.info("批量{},status={},ids={}",status==1?"起售":"停售",status,ids);
        dishService.updateStatus(status,ids);
        return  Result.success();
    }

    /**
     * （批量）删除
     */
    @DeleteMapping
    public Result delete(@RequestParam("ids") List<Long> ids){
        log.info("批量删除ids={}",ids);
        //TODO 改逻辑
        dishService.deleteWithFlavors(ids);
        return Result.success();
    }
}

